import {showToast} from "./tool";
import {miError} from "./exception";

export const setStorage = (key, value) => {
    return  new Promise((resolve, reject) => {
        wx.setStorage({
            key: key,
            data: value,
            encrypt: true, // 若开启加密存储，setStorage 和 getStorage 需要同时声明 encrypt 的值为 true
            success() {
                resolve()
            },
            fail(err){
                reject(err)
                throw miError(err.errMsg)
            }
        })
    })
}

export const getStorage = (key) => {

    return new Promise((resolve, reject) => {
        wx.getStorage({
            key: key,
            encrypt: true, // 若开启加密存储，setStorage 和 getStorage 需要同时声明 encrypt 的值为 true
            success(res) {
                resolve(res)

            },
            fail(err){
                reject(err)
                throw miError(err.errMsg)
            },
        })
    })


}

export const setStorageSync = (key, value) => {

    try {
        wx.setStorageSync(key, value)
    } catch (e) {
        throw miError(e.message)
    }

}

export const getStorageSync = (key) => {

    try {
        return wx.getStorageSync(key)
    } catch (e) {
        throw new miError(e.message)
    }

}

export const removeStorageSync = (key) => {
    try {
        wx.removeStorageSync(key)
    } catch (e) {
        throw miError(e.message)
    }
}
